# -*- coding: utf-8 -*-
import scrapy
import json

class ZhaopinSpider(scrapy.Spider):
    name = 'job'
    allowed_domains = ['zhaopin.com']
    # 目标网址
    start_url = 'https://fe-api.zhaopin.com/c/i/sou?start={start}&pageSize=90&cityId=822&salary=0,0&workExperience=-1&education=-1&companyType=-1&employmentType=-1&jobWelfareTag=-1&kw=%E4%BA%92%E8%81%94%E7%BD%91&kt=3&=0&_v=0.05023042&x-zp-page-request-id=59ac117c3df942fca100f3f0bb13f41f-1572230879011-306352&x-zp-client-id=b842f9f8-85e2-466a-fb89-86266cce73a2'



    # 发送请求
    def start_requests(self):
        # 每次返回个数为90，每次偏移量也为90，定义最大偏移量为10000，多请求了也没事，反正返回的没数据
        for start in range(0,10000,90):
            yield scrapy.Request(self.start_url.format(start=start))

    def parse(self, response):
        # 将招聘信息装载为json数据
        infos = json.loads(response.text)
        # 详细的招聘信息列表
        results = infos.get("data").get("results")

        # 遍历每个招聘信息
        for result in results:
            job_name = result.get("jobName")                                # 岗位名称
            company_name = result.get("company").get("name")                # 公司名称
            company_type = result.get("company").get("type").get("name")    # 公司类型
            company_size = result.get("company").get("size").get("name")    # 公司规模
            salary = result.get("salary")                                   # 薪水待遇
            welfare = "，".join(result.get("welfare"))                      # 公司福利
            working_exp = result.get("workingExp").get("name")              # 工作经验要求
            edu_level = result.get("eduLevel").get("name")                  # 学历要求
            city = result.get("city").get("display")                        # 公司所在城市
            time_state = result.get("timeState")                            # 招聘状态

            # 将所得信息封装为字典数据
            info = {}
            info['job_name'] = job_name
            info['company_name'] = company_name
            info['city'] = city
            info['company_type'] = company_type
            info['company_size'] = company_size
            info['salary'] = salary
            info['working_exp'] = working_exp
            info['edu_level'] = edu_level
            info['time_state'] = time_state
            info['welfare'] = welfare

            yield info